//
//  YWSelectWeekTableViewController.m
//  maintain
//
//  Created by Kyo on 16/10/10.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWSelectWeekTableViewController.h"
#import "UIColor+CustomColors.h"
#import "YWMacro.h"

@interface YWSelectWeekTableViewController ()
@property (nonatomic, strong) NSArray *dataSource;
@property (nonatomic, strong) NSMutableArray *selectWeekArray;
@end

@implementation YWSelectWeekTableViewController
- (instancetype)init{
    return [super  initWithStyle:UITableViewStyleGrouped];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //self.title = @"选择巡更日";//@"选择巡查日"
    self.view.backgroundColor = [UIColor customBackgroudColor];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"finish", nil) style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonItemEvent:)];
    
    _dataSource = [[NSArray alloc] initWithObjects:NSLocalizedString(@"monday", nil),@"周二",@"周三",@"周四",@"周五",@"周六",@"周日", nil];
    if (!_selectWeekArray) {
        _selectWeekArray = [NSMutableArray array];
    }
    [self initTableView];
}

- (void)initTableView{
    self.tableView.backgroundColor = [UIColor customBackgroudColor];
    self.tableView.separatorColor = [UIColor customLineColor];
    self.tableView.sectionFooterHeight = 1.0;
    [self.tableView setEditing:YES];
}

- (NSMutableArray *)selectWeekArray{
    if (!_selectWeekArray) {
        _selectWeekArray = [NSMutableArray array];
    }
    return _selectWeekArray;
}

- (void)setOwnWeekArray:(NSArray *)ownWeekArray{
    [self.selectWeekArray removeAllObjects];
    [self.selectWeekArray addObjectsFromArray:ownWeekArray];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [_dataSource count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reuseIdentifier = @"YWselectWeekReuseIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (cell==nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        cell.textLabel.font = kContentFont;
    }
    cell.textLabel.text = _dataSource[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *week = _dataSource[indexPath.row];
    if ([_selectWeekArray containsObject:week]) {
        [tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
    }else{
        [tableView deselectRowAtIndexPath:indexPath animated:NO];
    }
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *week = _dataSource[indexPath.row];
    //多选
    if (![_selectWeekArray containsObject:week]) {
        [_selectWeekArray addObject:week];
    }
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *week = _dataSource[indexPath.row];
    if ([_selectWeekArray containsObject:week]) {
        [_selectWeekArray removeObject:week];
    }
}

- (void)rightBarButtonItemEvent:(id)sender{
    if ([self.delegate respondsToSelector:@selector(selectWeeks:)]) {
        [self.delegate selectWeeks:_selectWeekArray];
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
